<?php

namespace Admin\Controller;

use Think\Controller;
use Home\Controller\CaptchaController;
use Think\Crypt;

/**
 * Description of LoginController
 *
 * @author xtceetg
 */
class LoginController extends Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->display();
    }

    public function login() {
        //var_dump($_POST);
        $username = I('post.username');
        $password = I('post.password');
        $captcha = I('post.captcha');
        if ($username == '' || $password == '' || $captcha == '') {
            $this->redirect('login/index');
        }
        $verify = new CaptchaController();
        if (!$verify->check_verify($captcha)) {
            $this->redirect('login/index');
        }
        $adminModel = new \Admin\Model\AdminModel();
        $admin = $adminModel->getByAdminUser($username);
        if ($admin) {
            if (!Crypt::Crypt_Hash_Compare($admin['password'], $password)) {
                $this->redirect('login/index');
            } else {
                //登录成功                    
                session("[start]");
                session('admininfo', $admin);
                $this->redirect('Index/index');
            }
        } else {
            $this->redirect('login/index');
        }
    }

    public function loginout() {
        session("[start]");
        $_SESSION['admininfo']=null;
        unset($_SESSION['admininfo']);
        $this->redirect("index/index");
    }

}
